home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / graphics / gnuplot / term / compact.c < prev    next >
C/C++ Source or Header  |  1993-05-11  |  2KB  |  101 lines

  1. /* compact.c -- contains routines to compress a vector stream without
  2. modifying it */
  3.  
  4. #ifndef COMPACT
  5.  
  6. /* replaces runs of constant slope in the buffer with single vectors 
  7.    returns the number of points eliminated */
  8. int compact_slope (xp,yp,isa_move,sz,delta)
  9. int xp[], yp[], isa_move[];
  10. int *sz;
  11. float delta;
  12. {
  13.     int dx,dy,old_size,new_index,i,start;
  14.     float slope,old_slope;
  15.  
  16.     old_size = *sz;
  17.     new_index = 0;
  18.     start = 0;
  19.     if (xp[1]!=xp[0])
  20.         old_slope = (float)(yp[1]-yp[0])/(float)(xp[1]-xp[0]);
  21.     else
  22.         old_slope = (float)(yp[1]-yp[0])/(float)(0.00001+xp[1]-xp[0]);
  23.     for (i=2;i<old_size;i++){
  24.         dx = xp[i] - xp[i-1];
  25.         dy = yp[i] - yp[i-1];
  26.         if (dx!=0)
  27.             slope = (float) dy / (float) dx;
  28.         else
  29.             slope = (float) dy / ((float) dx + 0.00001);
  30.         if ((abs(slope-old_slope) > delta)||(isa_move[i])){    
  31.             xp[new_index] = xp[start];
  32.             yp[new_index] = yp[start];
  33.             isa_move[new_index] = isa_move[start];
  34.             new_index++;
  35.             if (start != i-1){
  36.                 xp[new_index] = xp[i-1];
  37.                 yp[new_index] = yp[i-1];
  38.                 isa_move[new_index] = isa_move[i-1];
  39.                 new_index++;
  40.             }
  41.             start = i;
  42.             /* this is the slope for the new run */
  43.             old_slope = slope;
  44.         }
  45.     }
  46.     /* copy the last point into the new array */
  47.     xp[new_index] = xp[old_size-1];
  48.     yp[new_index] = yp[old_size-1];
  49.     isa_move[new_index] = isa_move[old_size-1];
  50.     new_index++;
  51.     *sz = new_index;
  52.     return (old_size - *sz);
  53. }
  54.  
  55. /* compacts the vector list by compressing runs of constant 
  56.    dx&dy into one vector
  57.    use this if floating point is too expensive!
  58.    more naive than compact_slope; doesn't compact as much as possible
  59.    returns the number of points eliminated */
  60. int compact_int(xp,yp,isa_move,size)
  61. int xp[],yp[], isa_move[], *size;
  62. {
  63.     int dx,dy,old_dx,old_dy,start,index,i,old_size;
  64.  
  65.     start = index = 0;
  66.     old_dx = xp[1]-xp[0];
  67.     old_dy = yp[1]-yp[0];
  68.     for (i=2;i<*size;i++){
  69.         dx = xp[i]-xp[i-1];
  70.         dy = yp[i]-yp[i-1];
  71.         if ((abs(dx-old_dx)+abs(dy-old_dy)!=0)||(isa_move[i])){
  72.             /*  we've reached the end of a run */
  73.             xp[index] = xp[start];
  74.             yp[index] = yp[start];
  75.             isa_move[index] = isa_move[start];
  76.             index++;
  77.             if (start != i-1){
  78.                 xp[index] = xp[i-1];
  79.                 yp[index] = yp[i-1];
  80.                 isa_move[index] = isa_move[i-1];
  81.                 index++;
  82.             }
  83.             start = i;
  84.             old_dx = dx;
  85.             old_dy = dy;
  86.         }
  87.     }  /* end for */
  88.     /* include the last point */
  89.     xp[index] = xp[*size-1];
  90.     yp[index] = yp[*size-1];
  91.     isa_move[index] = isa_move[*size-1];
  92.     index++;
  93.     old_size = *size;
  94.     *size = index;
  95.     return(old_size - *size);
  96. }
  97. #endif
  98.  
  99. #define COMPACT
  100.  
  101.